package com.dareway.reflex;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

public class Class_reflex_danger {
    public static void main(String[] args) throws ReflectiveOperationException {
        List<String> i = new ArrayList<>();

        Field field = ArrayList.class.getDeclaredField("size");
        field.setAccessible(true);
        field.set(i, 10);

        i.add("测试");   //只添加一个元素
        System.out.println(i.size());  //大小直接变成11
        i.remove(10);   //瞎移除都不带报错的，淦


        Integer integer = 10;

        Field fieldInt = Integer.class.getDeclaredField("value");

        Field modifiersField = Field.class.getDeclaredField("modifiers");  //这里要获取Field类的modifiers字段进行修改
        modifiersField.setAccessible(true);
        modifiersField.setInt(fieldInt,field.getModifiers()&~Modifier.FINAL);  //去除final标记

        fieldInt.setAccessible(true);
        fieldInt.set(integer, 100);   //强行设置值

        System.out.println(integer);
    }
}
